package aws;

import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * 依赖链
 */
public class DepList {

    private String[] depList(Map<String, String[]> pkgGraph, String pkg) {
        Queue<String> queue = new ArrayDeque<>();
        if(StringUtils.isEmpty(pkg)) {
            return new String[]{};
        }
        queue.add(pkg);
        List<String> list = new ArrayList<>();
        while (!queue.isEmpty()) {
            String element = queue.poll();
            list.add(element);
            String [] str = pkgGraph.get(element);
            Arrays.stream(str).forEach(item->{
                if(!queue.contains(item)) {
                    queue.add(item);
                }
            });
        }
        Collections.reverse(list);
        String [] arr = list.toArray(new String[list.size()]);

        System.out.println(String.join(",", arr));
        return arr;
    }
    public static void main(String[] args) {
        Map<String, String[]> map = new HashMap<>();
        map.put("A", new String[]{"B"});
        map.put("B", new String[]{"C","D"});
        map.put("C", new String[]{"D"});
        map.put("D", new String[]{"F"});
        map.put("E", new String[]{"D"});
        map.put("F", new String[]{});



        new DepList().depList(map, "B");
        new DepList().depList(map, "A");

    }

}
